package com.zkingsoft.rhi.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkingsoft.rhi.entity.MovableHistoricalRelics;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.service.MovableHistoricalRelicsService;
import com.zkingsoft.rhi.utils.MyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 可移动文物表 前端控制器
 * </p>
 *
 * @author xieguo
 * @since 2020-11-04
 */
@Api(tags = "可移动文物")
@RestController
@RequestMapping("/rhi/movable")
public class MovableHistoricalRelicsController {

    @Autowired
    MovableHistoricalRelicsService mhrs;

    @Autowired
    MyUtils myUtils;

    /**
     * @param mhr
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：查询可移动文物，可搜索
     * @date 2020/11/10 11:19
     */
    @ApiOperation(value = "查询可移动文物，可搜索")
    @PostMapping("/select")
    public JSONObject selectMovable(@RequestBody JSONObject mhr) {
        Integer offset = (Integer) mhr.get("offset");
        Integer limit = (Integer) mhr.get("limit");
        String name = (String) mhr.get("name");
        Integer souId = (Integer) mhr.get("souId");
        if (offset != null && limit != null && offset > 0 && limit > 0) {
            Page<MovableHistoricalRelics> mhrPage = new Page<>(offset, limit);
            QueryWrapper<MovableHistoricalRelics> mhrqw = new QueryWrapper<>();
            if (null != name) {
                mhrqw.like("name", name);
            }

            if (null != souId && souId > 0) {
                mhrqw.eq("sou_id", souId);
            }
            IPage<MovableHistoricalRelics> page = mhrs.page(mhrPage, mhrqw);
            Integer total = (int) page.getTotal();
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", page.getRecords(), total);
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "参数错误");
        }
    }

    /**
     * @param mhr
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：新增可移动文物
     * @date 2020/11/10 11:23
     */
    @ApiOperation(value = "新增可移动文物")
    @PostMapping("/insert")
    public JSONObject insertMovable(@RequestBody MovableHistoricalRelics mhr) {
        if (null==mhr.getRegistrationNumber()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }

        if (ObjectUtil.isEmpty(getList(mhr))){
            boolean save = mhrs.save(mhr);
            if (save) {
                return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "保存成功");
            } else {
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "保存失败");
            }
        }else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"藏品登记号不能重复");
        }

    }




    /**
     * @param mhr
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：更新可移动文物
     * @date 2020/11/10 11:23
     */
    @ApiOperation(value = "更新可移动文物")
    @PostMapping("/update")
    public JSONObject updateMovable(@RequestBody MovableHistoricalRelics mhr) {
        if (null==mhr.getRegistrationNumber()){
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"参数错误");
        }

        MovableHistoricalRelics oldMhr = mhrs.getById(mhr.getId());
        if (!mhr.getRegistrationNumber().equals(oldMhr.getRegistrationNumber())){
            QueryWrapper<MovableHistoricalRelics> wrapper = new QueryWrapper<>();
            wrapper.eq("registration_number",mhr.getRegistrationNumber());
            List<MovableHistoricalRelics> list = mhrs.list(wrapper);
            if (ObjectUtil.isNotEmpty(list)){
                return Result.setResultByCodeAndMsg(Result.STATUS_FAIL,"文物编码不能重复");
            }
        }

        boolean update = mhrs.updateById(mhr);
        if (update) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "更新成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "更新失败");
        }

    }

    public List<MovableHistoricalRelics> getList(MovableHistoricalRelics mhr){
        QueryWrapper<MovableHistoricalRelics> mhrqw = new QueryWrapper<>();
        mhrqw.eq("registration_number",mhr.getRegistrationNumber());
        return mhrs.list(mhrqw);
    }


    /**
     * @param ids
     * @return com.alibaba.fastjson.JSONObject
     * @author xieguo
     * 功能描述：删除可移动文物，可批量
     * @date 2020/11/10 11:23
     */
    @ApiOperation(value = "删除可移动文物，可批量")
    @GetMapping("/delete/{ids}")
    public JSONObject deleteMovable(@PathVariable("ids") String ids) {
        List<Integer> serverIds = myUtils.handleIds(ids);
        boolean delete = mhrs.removeByIds(serverIds);
        if (delete) {
            return Result.setResultByCodeAndMsg(Result.STATUS_SUCCESS, "删除成功");
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "删除失败");
        }
    }

    @ApiOperation(value = "回显")
    @GetMapping("/selectMovableByIds/{id}")
    public JSONObject selectMovableById(@PathVariable String id) {
        MovableHistoricalRelics movable = mhrs.getById(id);
        return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", movable, null);
    }
}

